package client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class StreamReader implements Runnable {

	private Socket socket;
	private IController controller;
	
	public StreamReader(Socket socket,IController controller) {
	
		this.socket = socket;
		this.controller=controller;
	}
	
	@Override
	public void run() {
		
		BufferedReader br = null;
		boolean check=true;
		String read="";
		
		try {
			br=new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF8"));
			
		} catch (IOException e) {
			System.err.println("Errore nella creazione dell'InputStream");
			check=false;
		}
		
		while(check){
			
			try {
				read=br.readLine();
				
				if(read==null){
					check=false;
					
				}
				
				controller.controll(socket,read);
					
				}
			 catch (IOException e) {
				check=false;
				System.err.println("Errore nella lettura dello Stream");
						
			 }
		}
		
	}
}